package com.star.common.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;


/**
 * Jackson工具类
 *
 * @author yangzj
 */
@Slf4j
public final class Json {
    private static final ObjectMapper MAPPER = new ObjectMapper();
    private static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";

    static {
        //对象的所有字段全部列入
        MAPPER.setSerializationInclusion(JsonInclude.Include.ALWAYS);
        //取消默认转换timestamps形式
        MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        //忽略空Bean转json的错误
        MAPPER.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        //所有的日期格式都统一为以下的样式，即yyyy-MM-dd HH:mm:ss
        MAPPER.setDateFormat(new SimpleDateFormat(STANDARD_FORMAT));
        //忽略 在json字符串中存在，但是在java对象中不存在对应属性的情况。防止错误
        MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    private Json() {
    }

    /**
     * 对象 转 Json格式字符串
     *
     * @param obj 对象
     * @return Json格式字符串
     */
    public static String toString(Object obj) {
        try {
            return obj instanceof String ? (String) obj : MAPPER.writeValueAsString(obj);
        } catch (Exception e) {
            log.error("[Json]toString,obj={}", obj, e);
            throw new RuntimeException("Json toString错误");
        }
    }

    /**
     * Json字符串 转 Bean
     *
     * @param text  json字符串
     * @param clazz 对象类型class
     * @param <T>   对象类型
     * @return 对象类型
     */
    public static <T> T toBean(String text, Class<T> clazz) {
        try {
            return MAPPER.readValue(text, clazz);
        } catch (Exception e) {
            log.error("[Json]toBean,text={}", text, e);
            throw new RuntimeException("Json toBean错误");
        }
    }


    /**
     * Json 转 Map
     *
     * @param text json
     * @param <K>  key
     * @param <V>  value
     * @return map
     */
    public static <K, V> Map<K, V> toMap(String text) {
        try {
            return toObject(text, new TypeReference<Map<K, V>>() {
            });
        } catch (Exception e) {
            log.error("[Json]toMap,text={}", text, e);
            throw new RuntimeException("Json toMap错误");
        }
    }


    /**
     * Json 转 List, Class 集合中泛型的类型，非集合本身
     *
     * @param text json
     * @param <T>  对象类型
     * @return List
     */
    public static <T> List<T> toList(String text) {
        try {
            return toObject(text, new TypeReference<List<T>>() {
            });
        } catch (Exception e) {
            log.error("[Json]toList,text={}", text, e);
            throw new RuntimeException("Json toList错误");
        }
    }

    /**
     * Json 转 Object
     *
     * @param text          json
     * @param typeReference TypeReference
     * @return T
     */
    public static <T> T toObject(String text, TypeReference<T> typeReference) {
        try {
            return MAPPER.readValue(text, typeReference);
        } catch (Exception e) {
            log.error("[Json]toObject,text={}", text, e);
            throw new RuntimeException("Json toObject错误");
        }
    }

}
